/**
 * 
 */
package edu.gatech.oad.yuno;

import java.util.HashMap;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * Model representing a user account
 * @author michael
 *
 */
public class User extends Model {
	
	public User(long id) {
		if (Model.context == null) Model.context = YUNoDosApplication.getAppContext();
		this.id = id;
		this.table_name = "users";
		this.data = new HashMap<String,Object>();
		this.load();
	}
	
	public User() {
		this(0);
	}

	public void validate() throws ValidationException {
		if (getUsername() == null || getUsername().equals(""))
			throw new ValidationException("Username cannot be blank.");
		if (getPassword() == null || getPassword().equals(""))
			throw new ValidationException("Password cannot be blank.");
		if (getEmail() == null || getEmail().equals(""))
			throw new ValidationException("Email cannot be blank.");
		if (getName() == null || getName().equals(""))
			throw new ValidationException("Name cannot be blank.");
	}
	

	/**
	 * Attempt to login with the given username and password.
	 * @param username
	 * @param password
	 * @return true if login was successful, false if either username or
	 * password is incorrect
	 */
	public static boolean login(String username, String password) {
		if (username == null) return false;
		User user = User.find(username);
		if (user == null) return false;
		return user.checkPassword(password);
	}
	
	/**
	 * Check if the given password matches the saved hash for this user
	 * @param password
	 * @return true if password matches, false otherwise
	 */
	public boolean checkPassword(String password) {
		return (password.equals(this.data.get("password"))); //TODO - check hash
	}
	
	/**
	 * Find a User in the database by username
	 * @param name
	 * @return the User if found, null if not found
	 */
	public static User find(String name) {
		if (Model.context == null) Model.context = YUNoDosApplication.getAppContext();
		if (name == null) return null;
		TodoListOpenHelper helper = new TodoListOpenHelper(Model.context);
		SQLiteDatabase db = helper.getReadableDatabase();
		String cols[] = {"id"};
		String args[] = {name};
		
		Cursor c = db.query("users", cols, "username=?", args, null, null, null, "1");
		if (c == null || c.getCount() < 1) {
			db.close();
			return null;
		};
		c.moveToFirst();
		long foundId = c.getLong(c.getColumnIndex("id"));
		
		db.close();
		return new User(foundId);
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return (String) data.get("username");
	}

	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		data.put("username", username);
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return (String) data.get("password");
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		data.put("password", password);
	}
	
	public String resetPassword(){
		String p = "";
		for (int i = 3; i < 10; i++){
			int height = (int) (Math.random()*10+i);
			p += Integer.toString(height);
		}
		return p;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return (String) data.get("email");
	}

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		data.put("email", email);
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return (String) data.get("name");
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		data.put("name", name);
	}
}
